package com.blog.login.controller;

import com.blog.login.service.UserService;
import com.blog.utils.JWTUtils;
import com.blog.utils.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 * @version 1.0 <br/>
 * <p style="font-family:SimSun;font-weight:bold">时间:<br/>2021/12/9 21:20</p>
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*")
@RequestMapping("/user")
public class UpdatePasswordController {
    @Autowired
    UserService userService;
    @PutMapping("/updatePassword")
    public ResponseResult UpdatePassword(@RequestHeader("token") String token, String username, String password){
        if(!JWTUtils.checkToken(username,token)){
            return ResponseResult.error().message("检测到用户未登录或登录信息已过期，请登录后再进行操作");
        }
        if(!userService.updatePassword(username, password)){
            log.info("用户："+username+" 修改密码失败");
            return ResponseResult.error().message("密码修改失败");
        }
        log.info("用户："+username+" 修改密码成功");
        JWTUtils.refreshJwtToken(username, null, null, null, null, null);
        return ResponseResult.ok().message("密码修改成功");
    }
}
